package com.ellecity06.common.base

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.WindowManager
import androidx.viewbinding.ViewBinding
import com.ellecity06.common.R
import com.ellecity06.common.utils.AppUtils


/**
 * @author ellecity06
 * @time 2023/4/1 9:44
 * @des
 */

abstract class BaseDialog<VB : ViewBinding>(context: Activity) :
    Dialog(context, R.style.DialogStyle) {
    val binding: VB by lazy { inflaterViewBinding() }
    private val mDisplayMetrics: DisplayMetrics by lazy { context.resources.displayMetrics }
    val screenWidth: Int by lazy { mDisplayMetrics.widthPixels }
    val screenHeight: Int by lazy { mDisplayMetrics.heightPixels }
    abstract fun inflaterViewBinding(): VB
    abstract fun initialize()
    open val widthP: Float = 0.94F
    open val gravity: Int = Gravity.CENTER
    open val animation: Int = -1
    open val isTouchOutsideDismiss: Boolean = true
    open val dimAmount: Float = 0.5f
    open val isCancelable: Boolean = true
    open val height:Int =  WindowManager.LayoutParams.WRAP_CONTENT
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initDialog()

        initialize()
    }

    open fun initDialog() {
        initAttributes((screenWidth * widthP).toInt())
    }

    /**
     * 初始化Dialog
     * @param width 设置宽度
     * @param height 设置高度
     */
    protected fun initAttributes(
        width: Int = WindowManager.LayoutParams.MATCH_PARENT, ) {
        val layoutParams = window!!.attributes
        when (width) {
            WindowManager.LayoutParams.MATCH_PARENT -> layoutParams.width =
                WindowManager.LayoutParams.MATCH_PARENT
            WindowManager.LayoutParams.WRAP_CONTENT -> layoutParams.width =
                WindowManager.LayoutParams.WRAP_CONTENT
            else -> layoutParams.width = width
        }
        when (height) {
            WindowManager.LayoutParams.MATCH_PARENT -> layoutParams.height =
                WindowManager.LayoutParams.MATCH_PARENT
            WindowManager.LayoutParams.WRAP_CONTENT -> layoutParams.height =
                WindowManager.LayoutParams.WRAP_CONTENT
            else -> layoutParams.height = height
        }
        //设置显示位置
        layoutParams.gravity = this.gravity
        //设置是否屏蔽返回键与点击空白区域不关闭Dialog
        setCancelable(isCancelable)
        setCanceledOnTouchOutside(isTouchOutsideDismiss)
        //设置是否开启动画
        if (this.animation > 0) {
            //如果动画资源为空,则设置成默认动画
            if (this.animation != 0) {
                layoutParams.windowAnimations = this.animation
            } else {
                layoutParams.windowAnimations = R.style.Animation_Design_BottomSheetDialog
            }
        }
        window!!.decorView.setPadding(0,0,0,0)
        layoutParams.dimAmount = dimAmount
        //设置属性
        window!!.attributes = layoutParams
    }


    override fun show() {
        AppUtils.getInstance().dialogShowing = true
        super.show()

    }

    override fun dismiss() {
        AppUtils.getInstance().dialogShowing = false
        super.dismiss()
    }

}